package com.mjh.convert;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mjh.bean.Product;
import com.mjh.service.ProductService;
import com.mjh.utils.ApplicationContextHolder;


public class IntentationProductConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取单元格内容
        String productName = cellData.getStringValue();
        // 从spring容器中获取指定的bean对象
        ProductService productService = ApplicationContextHolder.getBean(ProductService.class);
        // 根据商品名称查询商品
        Product product = productService.queryProductByName(productName);

        // 判断是否有值
        if (ObjUtil.isNotEmpty(product)) {
            return product.getId();
        }
        return -1;
    }
}
